<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Using Groovy</title>
</head>

<body>

<h2>Writing Steps in Groovy</h2>

<p><a href="http://groovy.codehaus.org">Groovy</a> is a very powerful dynamic scripting language
that compiles directly to the JVM.</p>

<p>JBehave supports writing Steps classes directly in Groovy, using the <a
    href="javadoc/groovy/org/jbehave/core/steps/groovy/GroovyStepsFactory.html">GroovyStepsFactory</a>
    and providing the list of Groovy scripts as classpath resources:
</p>
<script type="syntaxhighlighter" class="brush: groovy">
<![CDATA[
public class GroovyStories extends JUnitStories {

    @Override
    public Configuration configuration() {
        return new MostUsefulConfiguration().useStoryReporterBuilder(new StoryReporterBuilder().withFormats(HTML));
    }

    @Override
    protected List<String> storyPaths() {
        return new StoryFinder()
                .findPaths(codeLocationFromClass(this.getClass()).getFile(), asList("**/*.story"), null);
    }

    @Override
    public InjectableStepsFactory stepsFactory() {
        GroovyResourceFinder resourceFinder = new GroovyResourceFinder(codeLocationFromPath("src/main/groovy"), "**/*.story", "");
        return new GroovyStepsFactory(configuration(), new GroovyContext(resourceFinder));
    }

}
]]>
</script>

<p>It is also possible to use the Groovy compiler to directly compile Java class files. If this is done, the setup is simpler.  The <a href="https://github.com/jbehave/jbehave-tutorial">JBehave Tutorial</a> shows a Maven POM file and a runtime configuration that compiles 
Groovy into Java bytecode before use.</p>


<p>The Groovy script needs to include annotated methods, just as you would in Java:</p>
<script type="syntaxhighlighter" class="brush: java">
import org.jbehave.core.annotations.Given
import org.jbehave.core.annotations.Then
import org.jbehave.core.annotations.When

import static junit.framework.Assert.assertNotNull;

class ExampleGroovySteps {

  @Given("a date of \$date")
  def aDate(Date date) {
    assertNotNull(date);
  }

  @When("\$days days pass")
  def daysPass(int days) {
    assertNotNull(days);
  }

  @Then("the date is \$date")
  def theDate(Date date) {
      assertNotNull(date);
  }

}
]]>
</script>

<h2>Using Annotations</h2>

<script type="syntaxhighlighter" class="brush: java">
@RunWith(GroovyAnnotatedEmbedderRunner.class)
@Configure()
@UsingEmbedder();
@UsingGroovy(classLoader = MyGroovyClassLoader.class, resourceFinder = MyGroovyResourceFinder.class)
public class AnnotatedEmbedderUsingGroovy extends InjectableEmbedder {

    @Test
    public void run() {
        injectedEmbedder().runStoriesAsPaths(storyPaths());
    }

    protected List<String> storyPaths() {
        return new StoryFinder()
                .findPaths(codeLocationFromClass(this.getClass()).getFile(), asList("**/*.story"), null);
    }
    
}
]]>
</script>

<div class="clear">
<hr />
</div>
</body>
</html>
